﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;


[Serializable()]
public class MultipleChoiceQuestion : Question, ISerializable
{
    private List<string> _answers;
    private int _correctanswer;

    public MultipleChoiceQuestion(int id, string text, List<string> answers, int answer)
    {
        this._id = id;
        this._text = text;
        this._answers = answers;
        this._correctanswer = answer;
    }

    public MultipleChoiceQuestion()
    {
        _answers = new List<string>();
    }

    public override string getQuestionHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<div class=\"question_multiple_choice\">");
        sb.Append("<span class=\"text\">" + _text + "</span>");
        sb.Append("<ol class=\"choiceList\">");
        foreach (string answer in _answers)
        {
            sb.Append("<li class=\"choice\">" + answer + "</li>");
        }
        sb.Append("</ol>");
        sb.Append("</div>");

        return sb.ToString();
    }

    public override string getQuestionWithAnswerHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<div class=\"question_multiple_choice\">");
        sb.Append("<span class=\"text\">" + _text + "</span>");
        sb.Append("<ol class=\"choiceList\">");
        for(int i = 0; i < _answers.Count; i++)
        {
            sb.Append("<li class=\"choice");
            if (i == _correctanswer)
                sb.Append(" answer\">");
            else
                sb.Append("\">");
            sb.Append(_answers[i]);
            sb.Append("</li>");
        }
        sb.Append("</ol>");
        sb.Append("</div>");

        return sb.ToString();
    }

    public override string getQuestionXml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<MultipleChoiceQuestion>");
        sb.Append("<text>" + _text + "</text>");
        foreach (string answer in _answers)
        {
            sb.Append("<choice>" + answer + "</choice>");
        }
        sb.Append("</MultipleChoiceQuestion>");

        return sb.ToString();
    }

    public override string getQuestionWithAnswerXml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<MultipleChoiceQuestion>");
        sb.Append("<text>" + _text + "</text>");
        foreach (string answer in _answers)
        {
            sb.Append("<choice>" + answer + "</choice>");
        }
        for (int i = 0; i < _answers.Count; i++)
        {
            sb.Append("<answer>");
            if (i == _correctanswer)
                sb.Append(i);

            sb.Append("</answer>");
        }
        sb.Append("</MultipleChoiceQuestion>");

        return sb.ToString();
    }

    public void addAnswer(string answerText)
    {
        _answers.Add(answerText);
    }

    public int Correctanswer
    {
        get { return _correctanswer; }
        set { _correctanswer = value; }
    }

    public List<string> Answers
    {
        get { return _answers; }
        set { _answers = value; }
    }

    public override string getCorrectAnswer()
    { 
        return _correctanswer.ToString();
    }

    /// <summary>
    /// Deserialization constructor
    /// </summary>
    /// <param name="info"></param>
    /// <param name="ctxt"></param>
    public MultipleChoiceQuestion(SerializationInfo info, StreamingContext ctxt)
    {
        _id = (int)info.GetValue("_id", typeof(int));
        _text = (String)info.GetValue("_text", typeof(String));
        _answers = (List<string>)info.GetValue("_answers", typeof(List<string>));
        _correctanswer = (int)info.GetValue("_correctanswer", typeof(int));
    }

    /// <summary>
    /// Serialization function
    /// </summary>
    /// <param name="info"></param>
    /// <param name="ctxt"></param>
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("_id", _id);
        info.AddValue("_text", _text);
        info.AddValue("_answers", _answers);
        info.AddValue("_correctanswer", _correctanswer);
    }
}
